package com.opesoft.pf.page;

import java.util.ArrayList;
import java.util.Date;
import java.util.Iterator;
import java.util.List;
import java.util.Set;
import java.util.Timer;
import java.util.TimerTask;

import org.apache.log4j.Logger;

/**
 * 用于定时清理进度管理器中失效的进度
 * @author Larlf
 *
 */
public class ProcessRateTimerTask extends TimerTask
{
	Logger log = Logger.getLogger(this.getClass());

	@Override
	public void run()
	{
		log.debug("Clear time out processRate [Begin]");

		try
		{
			long t = (new Date()).getTime() - ProcessManager.timeOut;

			List list = new ArrayList();
			Set keys = ProcessManager.processes.keySet();
			for(Iterator iterator = keys.iterator(); iterator.hasNext();)
			{
				try
				{
					Object key = iterator.next();
					ProcessRate pr = (ProcessRate) ProcessManager.processes.get(key);
					if(pr.startTime < t)
					{
						list.add(key);
					}
				}
				catch(Exception e)
				{
					log.warn(e);
				}
			}

			if(list.size() > 0)
			{
				for(int i = 0; i < list.size(); i++)
				{
					try
					{
						Object key = list.get(i);
						ProcessManager.processes.remove(key);
						log.debug("  remove : " + key);
					}
					catch(Exception e)
					{
						log.warn(e);
					}
				}
			}
		}
		catch(Exception e)
		{
			e.printStackTrace();
			log.error(e);
		}

		//启动下次清理进程的时间
		Timer timer = new Timer();
		timer.schedule(new ProcessRateTimerTask(), ProcessManager.clearTime);

		log.debug("Clear time out processRate [End]");
	}
}
